package com.ljy.controller.user;



import com.ljy.constant.JwtClaimsConstant;
import com.ljy.dto.UserLoginDTO;
import com.ljy.entity.User;
import com.ljy.properties.JwtProperties;
import com.ljy.result.Result;
import com.ljy.service.UserService;
import com.ljy.utills.JwtUtil;
import com.ljy.vo.UserLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user/user")

public class UserController {
    @Autowired
    UserService userService;
    @Autowired
    JwtProperties jwtProperties;

    /**
     * 微信登录
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO){
        //调用service获取到openid和用户信息
        User user = userService.login(userLoginDTO);
        //jwt令牌封装
        Map<String,Object> claims=new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID,user.getId());
        String jwt = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);
        //buildVO
        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .openid(user.getOpenid())
                .token(jwt)
                .build();
        return Result.success(userLoginVO);
    }
    @GetMapping
    public Result<User>getUser(){
        User user=userService.getUser();
        return Result.success(user);
    }
    @PostMapping
    public Result editUser(@RequestBody User user){
        userService.editUser(user);
        return Result.success();
    }



}
